#include "Entity.h"
#include <fstream>
#include <iostream>
#include "Settings.h"

Entity::Entity(void)
{
	m_type = TYPE_NONE;
	m_visual = NULL;
	m_visualId = -1;
}

Entity::Entity( const std::string& filename )
{
	m_type = TYPE_NONE;
	m_visual = NULL;
	m_visualId = -1;
	Load(filename);
}
Entity::~Entity(void)
{
	if (m_visual) {
		VisualPack::FreeVisualPack(m_visualId);
	}
}

bool Entity::Load( const std::string& filename )
{
	if(m_visual != NULL) 
		VisualPack::FreeVisualPack(m_visualId);

	std::ifstream file;
	file.open(filename.c_str());

	if (!file.is_open()) return false;
	
	std::string visual;
		
	file >> pos.x >> pos.y;
/*	file >> size.x >> size.y;*/
	file >> visual;

	std::string::iterator pos = visual.begin();
	visual.replace(pos,pos+12,"graphics/packs");

	m_visual = VisualPack::GetVisualPack(visual);
	m_animation.SetVisualPack(m_visual);
	m_visualId = VisualPack::GetGuid(visual);
	
	assert(m_visual && "Entity::Load() couldn't load visual pack!");

	return true;
}

void Entity::Update( uint16 diff )
{
	m_animation.Update(diff);
}